/*
 *    Copyright (C) 2011-2012 Hai Bison
 *
 *    See the file LICENSE at the root directory of this project for copying
 *    permission.
 */

package group.pals.mailgate;

import group.pals.mailgate.beans.ItemEmailAccount;
import group.pals.mailgate.beans.ItemEmailProvider;
import group.pals.mailgate.preferences.Preferences;
import group.pals.utils.sys.MessageBox;
import java.util.Properties;
import javax.mail.Session;
import javax.mail.Store;

/**
 *
 * @author Hai Bison
 */
public class EmailAccountTester {

    private final Preferences _Preferences;
    private final ItemEmailAccount EmailAccount;
    private final ItemEmailProvider EmailProvider;

    public EmailAccountTester(ItemEmailAccount emailAccount, ItemEmailProvider emailProvider) {
        this._Preferences = Preferences.getInstance();
        this.EmailAccount = emailAccount;
        this.EmailProvider = emailProvider;
    }

    public void test() {
        final DialogWaiting _DialogWaiting = new DialogWaiting(null, "Testing...");
        _DialogWaiting.doWait();

        new Thread() {

            @Override
            public void run() {
                try {
                    Properties props = new Properties();
                    props.setProperty("mail.store.protocol", "imaps");

                    //To use TLS
                    //    props.put("mail.imap.auth", "true"); 
                    //    props.put("mail.imap.starttls.enable", "true");

                    //set this session up to use SSL for IMAP connections
                    //        props.setProperty("mail.imaps.socketFactory.class", "group.pals.mailgate.utils.DummySSLSocketFactory");
                    props.setProperty("mail.imaps.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
                    //don't fallback to normal IMAP connections on failure.
                    props.setProperty("mail.imaps.socketFactory.fallback", "false");

                    props.setProperty("mail.imaps.ssl.enable", "true");
                    props.setProperty("mail.imaps.ssl.trust", EmailProvider.getImapHost());

                    // use the simap port for imap/ssl connections.
                    props.setProperty("mail.imaps.socketFactory.host", EmailProvider.getImapHost());
                    props.setProperty("mail.imaps.socketFactory.port", Integer.toString(EmailProvider.getImapPort()));

                    // timeout
                    props.setProperty("mail.imaps.timeout", Integer.toString(_Preferences.getConnectionTimeout()));
                    props.setProperty("mail.imaps.connectionpooltimeout", Integer.toString(_Preferences.getConnectionTimeout()));
                    props.setProperty("mail.imaps.connectiontimeout", Integer.toString(_Preferences.getConnectionTimeout()));
                    Session session = Session.getInstance(props);

                    try {
                        Store store = session.getStore();
                        store.connect(EmailProvider.getImapHost(), EmailProvider.getImapPort(),
                                EmailAccount.getEmailAddress(), EmailAccount.getPassword());
                        store.close();

                        MessageBox.showInfoMsg(null, null, String.format("E-mail \"%s\" is valid", EmailAccount));
                    } catch (Exception ex) {
                        MessageBox.showErrMsg(null, null,
                                String.format("E-mail \"%s\" tested failed:\n\"%s\"", EmailAccount, ex));
                    }
                } finally {
                    _DialogWaiting.dispose();
                }
            }//run()
        }.start();
    }
}
